<script setup lang="ts"> 
  import * as Icons from '@element-plus/icons-vue'
  import { toLine } from '../../../utils'
  import { useCopy } from '../../../hooks/useCopy'
  import { watch, ref } from 'vue'

  let props = defineProps<{
    title: String,
    visible: Boolean
  }>()

  let emits = defineEmits(['update:visible'])

  let dialogVisible = ref<Boolean>(props.visible)
    
  const clickItem = (item: string) => {
    let text = `<el-icon-${toLine(item)} />`
    useCopy(text)
    dialogVisible.value = false
  }

  const handleClick = () => {
    emits('update:visible', !props.visible)
  }

  watch(() => props.visible, val => {
    dialogVisible.value = val
  })

  watch(() => dialogVisible.value, val => {
    emits('update:visible', val)
  })

</script>
<template>
  <div class="choose-icon">
    <el-button @click="handleClick" type="primary">
      <slot></slot>
    </el-button>
    <el-dialog :title="title" v-model="dialogVisible">
      <div class="container">
        <div class="item" v-for="(item,index) in Object.keys(Icons)" :key="index" @click="clickItem(item)">
          <div class="icon">
            <component :is="`el-icon-${toLine(item)}`"></component>
          </div>
          <div class="text">{{item}}</div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<style lang="scss" scoped>
.container{
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .item{
    width: 20%;
    height: 70px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin-bottom: 15px;
    cursor: pointer;
    .icon{
      margin-bottom: 5px;
    }
    svg{
      width: 2em;
      height: 2em;
    }
  }
}

</style>